package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.AlarmVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for AlarmVO
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ds.home.database.vo.AlarmVO
 * @author MyEclipse Persistence Tools
 */
public class AlarmDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, AlarmDAO.class);

	// property constants
	public static final String CYCLE = "cycle";
	public static final String SENSORID = "sensorid";
	public static final String ALERTCONTENT = "alertcontent";
	public static final String COMFORT = "comfort";
	public static final String SCENEID = "sceneid";



	public  List<AlarmVO> loadByWhere(String where ){
		NativeQuery<AlarmVO> query = this.getSession().createNativeQuery("select * from ha_alarm "+where, AlarmVO.class);
		List<AlarmVO>  volist= query.getResultList();

		return volist;
	}


	public void update(AlarmVO transientInstance) {
		log.debug("saving AlarmVO instance");
		try {
		
			getSession().update(transientInstance);
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public void delete(AlarmVO persistentInstance) {
		log.debug("deleting AlarmVO instance");
		try {
			
			getSession().delete(persistentInstance);
			
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public AlarmVO findById(String id) {
		log.debug("getting AlarmVO instance with id: " + id);
		AlarmVO instance =null;
		try {
			 instance = (AlarmVO) getSession().get(
					"com.ds.home.database.vo.AlarmVO", id);
		
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		return instance;
	}
	
	

	public List<AlarmVO> findByExample(AlarmVO instance) {
		log.debug("finding AlarmVO instance by example");
		List queryList=null;
		try {
			 queryList = getSession()
					.createCriteria("com.ds.home.database.vo.AlarmVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ queryList.size());
		
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return queryList;
	}

	
	
	public List<AlarmVO> findByProperty(String propertyName, Object value) {
		log.debug("finding AlarmVO instance with property: " + propertyName
				+ ", value: " + value);
		List queryList=null;
		try {
			String queryString = "from AlarmVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return queryList;
	}
	
	public List<AlarmVO> findBySensorId(String sensorid) {
		return findByProperty(SENSORID, sensorid);
	}
	
	public List<AlarmVO> findBySceneId(String sceneid) {
		return findByProperty(SCENEID, sceneid);
	}

	public List<AlarmVO> findByCycle(Object cycle) {
		return findByProperty(CYCLE, cycle);
	}

	public List<AlarmVO> findByAlertcontent(Object alertcontent) {
		return findByProperty(ALERTCONTENT, alertcontent);
	}

	public List<AlarmVO> findByComfort(Object comfort) {
		return findByProperty(COMFORT, comfort);
	}



	public List<AlarmVO> findAll() {
		List queryList=null;
		log.debug("finding all AlarmVO instances");
		try {
			String queryString = "from AlarmVO";
			Query queryObject = getSession().createQuery(queryString);
			queryList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		
		return queryList;
	}



	public void insert(AlarmVO instance) {
		log.debug("insert AlarmVO instance");
		try {
			
			getSession().insert(instance);
			
			log.debug("insert successful");
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			throw re;
		}finally{
			close();
		}
	}

}